package com.lanren.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanren.hovertitlerecylerview.HoverTitleAdapter;

import java.util.List;


/**
 * @ClassName HomeBookAdapter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/6 20:20
 * @Version 1.0
 */
public class HoverAdapter extends HoverTitleAdapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;

    private Context mContext;
    private List<GroupBean> list;
    private OnListener onListener;

    public HoverAdapter(Context context, List<GroupBean> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public boolean isHoverPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    public interface OnListener {
        void onItemClick(int position);
    }

    public void setOnListener(OnListener onListener) {
        this.onListener = onListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_item, viewGroup, false));
        } else {
            return new ContentHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.main_content_item, viewGroup, false));
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (getItemViewType(i) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) viewHolder;
            titleHolder.title.setText(list.get(i).getName());
        } else {
            ContentHolder contentHolder = (ContentHolder) viewHolder;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).isGroup()) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        TextView content;

        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.content);
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {
        TextView title;

        public TitleHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
        }
    }
}
